home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / Test Code Folder / Problem 07 Test Code / Main.cp
Encoding:
Text File  |  1998-06-15  |  2.8 KB  |  113 lines  |  [TEXT/CWIE]

  1. #include "Solution.h"
  2.  
  3. #include "ProblemUtils.h"
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <Files.h>
  8. #include <Errors.h>
  9.  
  10. static OSErr WriteRouteToHandle( Handle output, UInt32ArrayHandle route )
  11. {
  12.     OSErr err;
  13.     char node[20];
  14.     long i;
  15.     UInt32 *p;
  16.     UInt32 count;
  17.  
  18.     if ( route ) {
  19.         HLock( (Handle)route );
  20.         count = GetHandleSize((Handle)route) / sizeof(UInt32);
  21.         p = *route;
  22.         for ( i = 1; i <= count; i++ ) {
  23.             sprintf( node, "%ld", p[i-1] );
  24.             err = ProblemWriteStringToHandle( output, node );
  25.             if ( err != noErr ) break;
  26.             if ( i < count ) {
  27.                 err = ProblemWriteStringToHandle( output, "," );
  28.                 if ( err != noErr ) break;
  29.             }
  30.         }
  31.         HUnlock( (Handle)route );
  32.         if ( err != noErr ) return err;
  33.         err = ProblemWriteLineToHandle( output, "" );
  34.     } else {
  35.         err = ProblemWriteLineToHandle( output, "No Route" );
  36.     }
  37.     return err;
  38. }
  39.  
  40. pascal OSErr CheckGraph( const FSSpec* infile, const FSSpec* outfile )
  41. {
  42.     OSErr err;
  43.     Handle data;
  44.     Handle result;
  45.     char line[MAX_LINE_LEN];
  46.     char *linep;
  47.     char command[MAX_LINE_LEN];
  48.     UInt32 node_count;
  49.     Handle graph;
  50.     UInt32ArrayHandle route;
  51.     UInt32 vertex1, vertex2;
  52.     
  53.     result = NewHandle( 0 );
  54.     err = MemError();
  55.     ProblemLogError( err, "CheckGraph: NewHandle: result" );
  56.  
  57.     err = ProblemFileRead( infile, &data );
  58.     ProblemLogError( err, "CheckGraph: ProblemFileRead" );
  59.     if ( err == noErr ) {
  60.         linep = line;
  61.         if ( !ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) || !ProblemGetUInt32( &linep, &node_count ) ) {
  62.             err = -1;
  63.             ProblemLogError( err, "CheckGraph: node_count" );
  64.         }
  65.     }
  66.     if ( err == noErr ) {
  67.         GraphInit( &graph, node_count );
  68.         
  69.         while ( (err == noErr) && ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) ) {
  70.             linep = line;
  71.             ProblemGetCString( &linep, command, MAX_LINE_LEN );
  72.             if ( strcmp( command, "ADD" ) == 0 ) {
  73.                 err = ProblemGetUInt32( &linep, &vertex1 ) ? noErr : -1;
  74.                 ProblemLogError( err, "CheckGraph: vertex1" );
  75.                 if ( err != noErr ) break;
  76.                 err = ProblemGetUInt32( &linep, &vertex2 ) ? noErr : -1;
  77.                 ProblemLogError( err, "CheckGraph: vertex2" );
  78.                 if ( err != noErr ) break;
  79.                 GraphAddDirectedEdge( graph, vertex1, vertex2 );
  80.             } else if ( strcmp( command, "FIND" ) == 0 ) {
  81.                 err = ProblemGetUInt32( &linep, &vertex1 ) ? noErr : -1;
  82.                 ProblemLogError( err, "CheckGraph: vertex1" );
  83.                 if ( err != noErr ) break;
  84.                 err = ProblemGetUInt32( &linep, &vertex2 ) ? noErr : -1;
  85.                 ProblemLogError( err, "CheckGraph: vertex2" );
  86.                 if ( err != noErr ) break;
  87.                 GraphFindRoute( graph, vertex1, vertex2, &route );
  88.                 err = WriteRouteToHandle( result, route );
  89.                 DisposeHandle( (Handle)route );
  90.             }
  91.             if ( err != noErr ) break;
  92.         }
  93.         
  94.         DisposeHandle( graph );
  95.     }
  96.     
  97.     err = ProblemFileWrite( outfile, result );
  98.     
  99.     DisposeHandle( result );
  100.     DisposeHandle( data );
  101.     return err;
  102. }
  103.  
  104. int main()
  105. {
  106.     printf( "Starting\n" );
  107.     
  108.     ProblemRunFileTests( CheckGraph );
  109.  
  110.     return 0;
  111. }
  112.  
  113.